package com.purchase.jd.login;

import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.purchase.util.HttpUtils;

import java.net.HttpCookie;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author saw
 * @date 2021年12月27 16:23
 * @description
 */
public class Mobile {

    public static List<HttpCookie> cookieList = new ArrayList<>();

    public static String token = "AAEAIL9RTyAlP74hfJuGCu_yjY_1LatBrNLYS07Z-VRvum3e0";

    public static String url = "https://un.m.jd.com/cgi-bin/app/appjmp";

    public static Map<String, String> headers = new HashMap<>();

    /**
    * 获取手机token
    *
    * @return
    */
    public static JSONObject getToken() {
        headers.put("User-Agent", "Dalvik/2.1.0 (Linux; U; Android 10; MI 8 UD MIUI/V12.5.2.0.QECCNXM)");
        JSONObject map = new JSONObject();
        map.put(
        "body",
        "{\"to\":\"https%3a%2f%2fplogin.m.jd.com%2fjd-mlogin%2fstatic%2fhtml%2fappjmp_blank.html\"}");
        HttpResponse response =
            HttpUtils.post(
            null,
            "https://api.m.jd.com/client.action?functionId=genToken&clientVersion=10.2.6&build=91563&client=android&partner=xiaomi001&oaid=f4e5a7874a0cffaf&eid=eidA2564812378sdWXy2YPIkRdG39VkOZmdTa1l8t8+oAeyohYgp+jGddesSZfPzeSglIE9McYylspM0fM+8dZlEksowkRJv8Jy8eabXbLl58Av3Ox1t&sdkVersion=29&lang=zh_CN&harmonyOs=0&networkType=wifi&uts=0f31TVRjBSvGaYitFq4b7smYQeUK6gt8wWIf5CrEGxhmaA8ASn6AnSPgaD7iPQWo01SvdFYHdHY8cWAHIxQ8F3%2Fd8biOEWQ4RVkFvOh1bwG34HolmCj12wwXyRihXoWwlEwf6%2FgHpUiOBT1BOA3qiqdbE8Ru67kIJD8cF0DVj%2FB0oyk3kTRtXhxTTq55mvB5iVT04G7g00hkdwQl4K6DVw%3D%3D&uemps=0-2&ext=%7B%22prstate%22%3A%220%22%7D&ef=1&ep=%7B%22hdid%22%3A%22JM9F1ywUPwflvMIpYPok0tt5k9kW4ArJEU3lfLhxBqw%3D%22%2C%22ts%22%3A1640589330711%2C%22ridx%22%3A-1%2C%22cipher%22%3A%7B%22area%22%3A%22CJZpCJCmC18zDNq0XzU5CJSz%22%2C%22d_model%22%3A%22JUu4VUG%3D%22%2C%22wifiBssid%22%3A%22dW5hbw93bq%3D%3D%22%2C%22osVersion%22%3A%22CJK%3D%22%2C%22d_brand%22%3A%22WQvrb21f%22%2C%22screen%22%3A%22CtKyEIenCNqm%22%2C%22uuid%22%3A%22ENY4Dtu1CNC3DtUnCzOmBWO0DJK0DwHuYzq3YG%3D%3D%22%2C%22aid%22%3A%22YzS2DWCmC2HsYzPsCJG4DG%3D%3D%22%2C%22openudid%22%3A%22YzS2DWCmC2HsYzPsCJG4DG%3D%3D%22%7D%2C%22ciphertype%22%3A5%2C%22version%22%3A%221.2.0%22%2C%22appname%22%3A%22com.jingdong.app.mall%22%7D&st=1640591325272&sign=0dab3e3c9c6c758b5f1428a1e5c7fa26&sv=110",
            map);
        cookieList.addAll(response.getCookies());
        System.out.println("获取token值："+response.body());
        return JSON.parseObject(response.body());
    }

    public static void main(String[] args) {
        JSONObject json = getToken();
        String url = json.getString("url");
        json.put("tokenKey", json.getString("tokenKey"));
        json.put("to", url);
        HttpResponse response = HttpUtils.get(url, json, null, cookieList);
        cookieList.addAll(response.getCookies());
        String nextUrl = response.header("location");
        System.out.println(nextUrl);
    }
}
